package com.yunji.order.dao.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunji.common.annotation.DataScope;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.utils.StringUtils;
import com.yunji.order.bo.GetAuditApplyBO;
import com.yunji.order.bo.GetOrderRefundBO;
import com.yunji.order.dao.IRefundDao;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.domain.RefundDO;
import com.yunji.order.dto.RefundParam;
import com.yunji.order.mapper.IRefundMapper;
import com.yunji.order.vo.RefundVO;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/16 15:29
 * @description : TODO
 **/
@Repository
public class RefundDaoImpl extends BaseDaoImpl<IRefundMapper, RefundDO> implements IRefundDao {

    @Override
    public boolean updateRefundStatus(Long refundId, Integer newStatus, List<Integer> oldStatusList) {
        return lambdaUpdate().set(RefundDO::getApplyStatus, newStatus)
                .eq(RefundDO::getRefundId, refundId)
                .in(RefundDO::getApplyStatus, oldStatusList).update();
    }

    @Override
    public List<RefundDO> listByOrderId(Long orderId) {
        return lambdaQuery().eq(RefundDO::getOrderId, orderId).list();
    }

    @Override
    public RefundDO getByCode(String refundCode) {
        return lambdaQuery().eq(RefundDO::getRefundCode, refundCode).one();
    }

    @Override
    public RefundDO getByOrderId(GetOrderRefundBO bo) {
        return lambdaQuery().eq(RefundDO::getOrderId, bo.getOrderId()).in(RefundDO::getApplyStatus, bo.getStatusList()).one();
    }


    @Override
    public List<RefundDO> listAuditRefund(GetAuditApplyBO bo) {
        return lambdaQuery().eq(RefundDO::getApplyStatus, bo.getApplyStatus())
                .ge(RefundDO::getApplyTime, bo.getTime()).le(RefundDO::getApplyTime, LocalDate.now()).list();
    }

    @DataScope
    @Override
    public List<RefundVO> refundList(RefundParam param) {
        if (param.getEndedApplyTime() != null)
            param.setEndedApplyTime(param.getEndedApplyTime().plusDays(1L));
        MPJLambdaWrapper<RefundDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(RefundDO.class).leftJoin(OrderDO.class, OrderDO::getOrderId, RefundDO::getOrderId)
                .select(OrderDO::getContainerName).select(OrderDO::getLocationName).select(OrderDO::getSiteName).select(OrderDO::getAgentName)
                .select(OrderDO::getMemberId).select(OrderDO::getMemberPhone).select(OrderDO::getMemberName).select(OrderDO::getLocationTypeName)
                .like(param.getOrderCode() != null, OrderDO::getOrderCode, param.getOrderCode())
                .like(StringUtils.isNotEmpty(param.getMemberPhone()), OrderDO::getMemberPhone, param.getMemberPhone())
                .like(param.getMemberName() != null, OrderDO::getMemberName, param.getMemberName())
                .like(param.getContainerName() != null, OrderDO::getContainerName, param.getContainerName())
                .like(param.getSiteName() != null, OrderDO::getSiteName, param.getSiteName())
                .like(param.getAgentName() != null, OrderDO::getAgentName, param.getAgentName())
                .eq(param.getApplyStatus() != null, RefundDO::getApplyStatus, param.getApplyStatus())
                .ge(param.getBeginApplyTime() != null, RefundDO::getApplyTime, param.getBeginApplyTime())
                .le(param.getEndedApplyTime() != null, RefundDO::getApplyTime, param.getEndedApplyTime())
                .eq(param.getOrderId() != null, RefundDO::getOrderId, param.getOrderId())
                .orderByDesc(RefundDO::getApplyTime);
        if (param.isExport())
            return selectJoinList(RefundVO.class, wrapper);
        return pageJoinList(RefundVO.class, param, wrapper);
    }
}
